package com.itcast.googleplay.holder;

import com.itcast.googleplay.R;
import com.itcast.googleplay.bean.AppInfo;
import com.itcast.googleplay.http.HttpApi;
import com.itcast.googleplay.utils.UIUtils;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class AppSafeHolder extends BaseHolder<AppInfo>{

	private ImageView[] imageSafe;
	private ImageView[] imageIcon;
	private TextView[] textViews;
	private LinearLayout[] ll;
	private DisplayImageOptions options;
	private LinearLayout llRoot;
	private boolean isOpen = false;
	private ValueAnimator valueAnimator;
	private LayoutParams layoutParams;
	private ImageView ivArrow;
	private LinearLayout llParent;

	@Override
	public void initData() {
		AppInfo appInfo = getData();
		for (int i = 0; i < 3; i++) {
			if(i<appInfo.safe.size()){
				textViews[i].setText(appInfo.safe.get(i).safeDes);
				ImageLoader.getInstance().displayImage
					(HttpApi.IMAGEURL+appInfo.safe.get(i).safeUrl, imageSafe[i], options);
				ImageLoader.getInstance().displayImage
					(HttpApi.IMAGEURL+appInfo.safe.get(i).safeDesUrl, imageIcon[i], options);
			}else{
				imageSafe[i].setVisibility(View.GONE);
				ll[i].setVisibility(View.GONE);
			}
		}
		getLongHeight();
	}

	private int getLongHeight() {
		llRoot.measure(0, 0);
		return llRoot.getMeasuredHeight();
	}

	@Override
	public View initView() {
		options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.ic_default)
			.showImageForEmptyUri(R.drawable.ic_default)
			.showImageOnFail(R.drawable.ic_default)
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)//是否允许让图片反转
			.build();
		
		View view = View.inflate(UIUtils.getContext(), R.layout.layout_app_safe, null);
		imageSafe = new ImageView[3];
		imageSafe[0] = (ImageView) view.findViewById(R.id.iv_safe1);
		imageSafe[1]  = (ImageView) view.findViewById(R.id.iv_safe2);
		imageSafe[2]  = (ImageView) view.findViewById(R.id.iv_safe3);
		
		imageIcon = new ImageView[3];
		imageIcon[0] = (ImageView) view.findViewById(R.id.image1);
		imageIcon[1]  = (ImageView)view.findViewById(R.id.image2);
		imageIcon[2]  = (ImageView)view.findViewById(R.id.image3);
		
		textViews = new TextView[3];
		textViews[0] = (TextView) view.findViewById(R.id.tv1);
		textViews[1] = (TextView)view.findViewById(R.id.tv2);
		textViews[2] = (TextView)view.findViewById(R.id.tv3);
		
		ll = new LinearLayout[3];
		ll[0] = (LinearLayout) view.findViewById(R.id.ll1);
		ll[1] = (LinearLayout)view.findViewById(R.id.ll2);
		ll[2] = (LinearLayout)view.findViewById(R.id.ll3);
		
		llRoot = (LinearLayout) view.findViewById(R.id.ll_root);
		llParent = (LinearLayout) view.findViewById(R.id.ll_parent);
		ivArrow = (ImageView) view.findViewById(R.id.iv_arrow);
		
		layoutParams = new LinearLayout.LayoutParams
			(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		layoutParams.height = 0;
		llRoot.setLayoutParams(layoutParams);
		
		llParent.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				expend();
			}
		});
		return view;
	}

	protected void expend() {
		if(!isOpen){
			isOpen = true;
			valueAnimator = ValueAnimator.ofInt(0,getLongHeight());
		}else{
			isOpen = false;
			valueAnimator = ValueAnimator.ofInt(getLongHeight(),0);
		}
		valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator valueAnimator) {
				layoutParams.height = (Integer) valueAnimator.getAnimatedValue();
				llRoot.setLayoutParams(layoutParams);
			}
		});
		
		valueAnimator.addListener(new AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animator arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animator arg0) {
				if(isOpen){
					ivArrow.setImageResource(R.drawable.arrow_up);
				}else{
					ivArrow.setImageResource(R.drawable.arrow_down);
				}
			}
			
			@Override
			public void onAnimationCancel(Animator arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		valueAnimator.setDuration(200);
		valueAnimator.start();
	}

}
